//
//  SXReplyViewController.m
//  81 - 网易新闻
//
//  Created by 董 尚先 on 15/2/8.
//  Copyright (c) 2015年 ShangxianDante. All rights reserved.
//

#import "SXReplyViewController.h"
#import "ServiceUtils.h"

@interface SXReplyViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) UITableView *tablview;
@property (strong, nonatomic) NSArray *replyArray;
@end


@implementation SXReplyViewController
@synthesize tablview,ID,replyArray,titlelabel,url_net;


- (void)backAction:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)NavInit{
    
    UIView * view = [[UIView alloc]initWithFrame:NSRect(0, 0, SCREEN_WIDTH, 64)];
    view.backgroundColor  = Maincolors;
    [self.view addSubview:view];
    
    [theAppDelegate settitle:self.view title:titlelabel];
    
    UIButton *backbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backbtn setFrame:CGRectMake(7, 28, 60/2, 50/2)];
    [backbtn setImage:[UIImage imageNamed:@"night_icon_back.png"] forState:UIControlStateNormal];
    [backbtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backbtn];
    
}

- (void)viewDidLoad{
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.tablview = [[UITableView alloc]initWithFrame:NSRect(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
    self.tablview.backgroundColor = [UIColor whiteColor];
    self.tablview.delegate  =self;
    self.tablview.dataSource = self;
    [self.view addSubview:self.tablview];
    [theAppDelegate setExtraCellLineHidden:self.tablview];
    
    [self NavInit];
    [self get_list];
}

#pragma mark - ******************** tbv数据源方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [self.replyArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    
    static NSString *CELLID = @"replyCell";
    
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELLID];
    if (cell == nil) {
       cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CELLID];
        
        UILabel * username = [[UILabel alloc]initWithFrame:NSRect(15, 5, SCREEN_WIDTH-30, 25)];
        username.font =Font(16);
        username.backgroundColor  =[UIColor clearColor];
        username.tag = 1;
        [cell.contentView addSubview:username];
        
        UILabel * time = [[UILabel alloc]initWithFrame:NSRect(SCREEN_WIDTH-120, 5, 110, 25)];
        time.font =Font(14);
        time.backgroundColor  =[UIColor clearColor];
        time.textColor =[UIColor darkGrayColor];
        time.textAlignment  =NSTextAlignmentRight;
        time.tag = 2;
        [cell.contentView addSubview:time];

        
        UILabel * content = [[UILabel alloc]initWithFrame:NSRect(15, 30, SCREEN_WIDTH-20, 40)];
        content.font =Font(15);
        content.backgroundColor  =[UIColor clearColor];
        content.tag = 3;
        content.numberOfLines = 2;
        [cell.contentView addSubview:content];

    
    }
    
     UILabel * username = (UILabel*)[cell viewWithTag:1];
     UILabel * time = (UILabel*)[cell viewWithTag:2];
     UILabel * content = (UILabel*)[cell viewWithTag:3];
    
    [username setText:[[self.replyArray objectAtIndex:indexPath.row] objectForKey:@"username"]];
    [time setText:[theAppDelegate confromTimesp:[[self.replyArray objectAtIndex:indexPath.row] objectForKey:@"creat_at"] :@"MM-dd HH-mm"]];
    [content setText:[[self.replyArray objectAtIndex:indexPath.row] objectForKey:@"content"]];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 80;
}

#pragma mark -  获取评论
- (void)get_list
{
    NSDictionary *dict;
    if([titlelabel isEqualToString:@"全部评论"]){
        
        dict = @{@"commentid": ID};
    }else{
        dict = @{@"userid": ID};
    }

    DebugLog(@"dict =  %@",dict);
    
    AFHTTPSessionManager *manager = [ServiceUtils commonHTTPManager];
    [manager GET:url_net parameters:dict progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        DebugLog(@"全部/我的 评论返回数据responseString = %@", operation.responseString);
        
        if (responseObject == nil) {
            DebugLog(@"json parse failed \r\n");
            return;
        }
        
        if([responseObject isKindOfClass:[NSDictionary class]]){
            DebugLog(@"字典");
            self.replyArray = [responseObject objectForKey:@"data"];
        }else if([responseObject isKindOfClass:[NSArray class]]){
            DebugLog(@"数组");
            self.replyArray = responseObject;
        }else{
            DebugLog(@"空");
            return;
        }
        
        [self.tablview reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        DebugLog(@"报错error =  %@", error);

    }];
}



@end
